/*
 * Copyright (c) 2015 elion. All Rights Reserved.
 */
package com.guoyu.project.web.admin;

import java.util.Map;

import javax.annotation.Resource;

import com.guoyu.project.model.UserInfo;
import com.guoyu.project.model.enums.AccountType;
import com.guoyu.project.service.UserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.JsonUtil;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Banner;
import com.guoyu.project.service.BannerService;
import com.guoyu.project.service.FileUploadService;

/**
 * banner信息,前端接口BannerController
 *
 * @author: xblibo Sat Jun 27 14:00:13 CST 2015
 * @version 1.0
 */
@Controller
@RequestMapping("admin/user")
public class UserInfoController {

    private final static Logger logger = LoggerFactory.getLogger(UserInfoController.class);

    @Resource
    private UserInfoService userInfoService;

    @RequestMapping("index")
    public String index() {
        return "admin/user/index";
    }

    /**
     * 用户信息页
     *
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Pagination<UserInfo> list(@RequestParam(defaultValue = "0") int offset,
            @RequestParam(defaultValue = "10") int limit, String userName, Model model) {
        Map<String, Object> params = Maps.newHashMap();
        if (StringUtils.isNotBlank(userName)) {
            params.put("userName", userName);
        }
        return userInfoService.queryUserInfos(offset, limit, params);
    }

    /**
     * 跳到添加用户信息填写页
     *
     * @return
     */
    @RequestMapping("add")
    public String add() {
        return "admin/user/add";
    }

    /**
     * 保存用户信息vip
     *
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public APIResponse save(UserInfo userInfo) {
        logger.info("提交添加用户信息：{}", JsonUtil.encode(userInfo));
        try {
            userInfoService.register(userInfo.getUserName(), userInfo.getPhone(), userInfo.getEmail(),
                    userInfo.getPassword(), AccountType.VIP);
        } catch (Exception e) {
            return APIResponse.returnFail(e.getMessage());
        }
        return APIResponse.returnSuccess();
    }
    
    @RequestMapping("addUpFile")
    public String addUpFile() {
        return "admin/user/addUpFile";
    }
    /**
     * 保存用户信息UpFile
     *
     * @return
     */
    @RequestMapping("saveUpFile")
    @ResponseBody
    public APIResponse saveUpFile(UserInfo userInfo) {
        logger.info("提交添加用户信息：{}", JsonUtil.encode(userInfo));
        try {
            userInfoService.register(userInfo.getUserName(), userInfo.getPhone(), userInfo.getEmail(),
                    userInfo.getPassword(), AccountType.UpFile);
        } catch (Exception e) {
            return APIResponse.returnFail(e.getMessage());
        }
        return APIResponse.returnSuccess();
    }
    

    /**
     * 删除用户信息
     *
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public APIResponse delete(int userId) {
        userInfoService.deleteById(userId);
        return APIResponse.returnSuccess();
    }

}